package com.googlecode.svnj.reports;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavResource;
import org.apache.jackrabbit.webdav.WebdavRequest;
import org.apache.jackrabbit.webdav.WebdavResponse;
import org.w3c.dom.Node;

import com.googlecode.svnj.RepoContext;
import com.googlecode.svnj.XMLUtil;
import com.googlecode.svnj.callbacks.LocationSegment;
import com.googlecode.svnj.callbacks.LocationSegmentCallback;
import com.googlecode.svnj.dav.IO;
import com.googlecode.svnj.dav.Path;
import com.googlecode.svnj.dav.SVNResource;

public class GetLocationSegmentsReport extends ReportRequest {

    private long pegRevision, startRevision, endRevision;
    private String path;
    
    @Override
    public String getReportType() {
        return TYPE_LOCATION_SEGMENTS;
    }

/*
 <S:get-location-segments xmlns:S="svn:" xmlns:D="DAV:">
<S:path></S:path>
<S:peg-revision>4</S:peg-revision>
<S:start-revision>4</S:start-revision>
<S:end-revision>0</S:end-revision>
</S:get-location-segments>
 */
    @Override
    protected void parse(Node root) {
        path = XMLUtil.getChildText(root, "path");
        pegRevision = XMLUtil.getChildLong(root, "peg-revision", -1L);
        startRevision = XMLUtil.getChildLong(root, "start-revision", -1L);
        endRevision = XMLUtil.getChildLong(root, "end-revision", -1L);
    }

    /*
<S:get-location-segments-report xmlns:S="svn:" xmlns:D="DAV:">
 <S:location-segment path="branches/1.0" range-start="3" range-end="4"/>
 <S:location-segment path="trunk" range-start="1" range-end="2"/>
</S:get-location-segments-report>
*/
    private static class ReportCallback extends StaxHelper
    implements LocationSegmentCallback {

        ReportCallback(XMLStreamWriter w) {
            stream = w;
        }
        public void singleSegment(LocationSegment seg) throws Exception {
            emptyElem("S:location-segment");
            att("path", seg.getPath());
            att("range-start", String.valueOf(seg.getRangeStart()));
            att("range-end", String.valueOf(seg.getRangeEnd()));
            //end(); empty elem, no need
        }
        
    }
    @Override
    public void report(WebdavRequest req, WebdavResponse rsp,
            DavResource resource) throws DavException, IOException,
            XMLStreamException {
        rsp.setHeader("Content-Type", "text/xml; charset=\"utf-8\"");
        OutputStream os = rsp.getOutputStream();
        XMLStreamWriter xs = XMLUtil.initWriter(os, "S:get-location-segments-report");
        RepoContext rc = IO.get().getRepo(req);
        // make them all full paths
        SVNResource svnRes = (SVNResource) resource;
        String reposPath = svnRes.getSpecialResource().getReposPath();
        String fullPath = path;
        if (reposPath != null) {
            fullPath = Path.join(reposPath, path);
        }
        ReportCallback cback = new ReportCallback(xs);
        rc.getLocationSegments(fullPath, pegRevision, startRevision, endRevision, cback);
        xs.writeEndDocument(); // be sure to close it out        
    }

}
